package com.example.springbootServiceNetwork.demos.web.Controller;

import com.example.springbootServiceNetwork.demos.web.Config.JwtInfo;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Config.ResultEnum;
import com.example.springbootServiceNetwork.demos.web.Dto.CreateOrderDTO;
import com.example.springbootServiceNetwork.demos.web.Service.JwtRedistService;
import com.example.springbootServiceNetwork.demos.web.Service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private JwtRedistService jwtRedistService;

    /**
     * 新增订单
     * @param dto
     * @param token
     * @return
     */
    @PostMapping("/addOrder")
    public Result addMerchant(@RequestBody CreateOrderDTO dto, @RequestHeader("token") String token) {
        Result result = new Result();
        try {
            JwtInfo jwtInfo = new JwtInfo();
            if (!(token.isEmpty())) {
                if (!(dto.getGoodsList().isEmpty())){
                    jwtInfo = jwtRedistService.getUserInfo(token);
                    return orderService.addOrder(dto, jwtInfo);
                }else {
                    result.setCode(ResultEnum.BADREQUEST.getCode());
                    result.setMsg("请求参数不足或不合法！");
                }
            } else {
                jwtInfo.setPass(false);
            }
        } catch (Exception e) {
            result.setCode(ResultEnum.FORBIDDEN.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 用户查询自己订单
     */

    /**
     * 修改订单
     */

    /**
     * 删除订单
     */


    /**
     * 管理端 查询所有订单
     */
}
